﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InputManager : Singleton<InputManager> 
{
	protected InputManager () {}

	public string[] m_ControllerButtonsName;
	public string[] m_ControllerAxisName;

	private KeyCode m_CurrentKeyPressed;
	private EventType m_LastEventType;
	
	private Dictionary<string, int> m_ButtonsPressedName;
	private int m_LastButtonsCount;

	private string[] m_JoystickNames;
	public string[] JoystickNames
	{
		get { return m_JoystickNames; }
	}

	public delegate void OnKeyPressed(KeyCode i_KeyPressed);
	public OnKeyPressed m_OnKeyPressed;
	
	public delegate void OnButtonPressed(string[] i_ButtonsPressed);
	public OnButtonPressed m_OnButtonPressed;

	private void Awake()
	{
		base.Awake();
		m_CurrentKeyPressed = KeyCode.None;
		m_LastEventType = EventType.ignore;
		m_ButtonsPressedName = new Dictionary<string, int>();
		m_LastButtonsCount = 0;

		m_JoystickNames = Input.GetJoystickNames();
	}

	private void Update()
	{
		if(m_JoystickNames.Length > 0)
		{
			CheckControllersButtons();
			CheckControllerAxis();
		}
	}

	private void OnGUI()
	{
		CheckKeyBoardAndMouseInputs();
	}

	private void CheckKeyBoardAndMouseInputs()
	{
		if(Event.current.type == EventType.mouseDown || Event.current.type == EventType.KeyDown)
		{
			if(m_CurrentKeyPressed == KeyCode.None)
			{
				if(Event.current.type == EventType.mouseDown)
				{
					m_CurrentKeyPressed = (KeyCode) System.Enum.Parse(typeof(KeyCode), "Mouse" + Event.current.button.ToString()) ;
				}
				else if(Event.current.type == EventType.KeyDown)
				{
					m_CurrentKeyPressed = Event.current.keyCode;
				}
				
				if(m_CurrentKeyPressed != KeyCode.None)
				{
					m_OnKeyPressed(m_CurrentKeyPressed);
				}
			}
			
			if(Event.current.type == EventType.mouseDown)
			{
				m_LastEventType = EventType.mouseDown;
			}
			else
			{
				m_LastEventType = EventType.KeyDown;
			}
		}
		else if((	Event.current.type == EventType.mouseUp && m_LastEventType == EventType.mouseDown)	|| 
		        (	Event.current.type == EventType.KeyUp && m_LastEventType == EventType.KeyDown))
		{
			m_LastEventType = EventType.ignore;
			m_CurrentKeyPressed = KeyCode.None;
		}
	}

	private void CheckControllersButtons()
	{
		for(int i = 0 ; i < m_JoystickNames.Length; i++)
		{
			foreach(string s in m_ControllerButtonsName)
			{
				string currentPlayerString = "P" + (i + 1).ToString() + s;
				if(Input.GetButton(currentPlayerString))
				{
					if(!m_ButtonsPressedName.ContainsKey(currentPlayerString))
					{
						m_ButtonsPressedName.Add(currentPlayerString, m_ButtonsPressedName.Count);
					}
				}
				else
				{
					if(m_ButtonsPressedName.ContainsKey(currentPlayerString))
					{
						SortButtonsInDictionnary(m_ButtonsPressedName[currentPlayerString]);
						m_ButtonsPressedName.Remove(currentPlayerString);
					}
				}
			}

			if(m_ButtonsPressedName.Count > 0 || (m_ButtonsPressedName.Count == 0 && m_LastButtonsCount != 0))
			{
				if(m_ButtonsPressedName.Count != m_LastButtonsCount && m_OnButtonPressed != null)
				{
					string[] buttonsPressedName = new string[17];

					foreach(KeyValuePair<string, int> kv in m_ButtonsPressedName)
					{
						buttonsPressedName.SetValue(kv.Key, kv.Value);
					}
					m_OnButtonPressed(buttonsPressedName);
				}

				m_LastButtonsCount = m_ButtonsPressedName.Count;
			}
		}
	}

	private void SortButtonsInDictionnary(int i_SliceIndex)
	{
		Dictionary<string, int> buffer = new Dictionary<string, int>(m_ButtonsPressedName);
		List<string> keys = new List<string>(m_ButtonsPressedName.Keys);
		foreach(string key in keys)
		{
			if(m_ButtonsPressedName[key] > i_SliceIndex)
			{
				int newValue = m_ButtonsPressedName[key] - 1;
				m_ButtonsPressedName[key] = newValue;
			}
		}
	}

	private void CheckControllerAxis()
	{
		for(int i = 0 ; i < m_JoystickNames.Length; i++)
		{
			foreach(string s in m_ControllerAxisName)
			{
				string currentPlayerString = "P" + (i + 1).ToString() + s;
				//Debug.Log(currentPlayerString + Input.GetAxis(currentPlayerString).ToString());
			}
		}
	}
}
